package com.hzdesk.media.koreabatchmatvector.request;

import android.content.Context;

import com.hzdesk.media.koreabatchmatvector.inter.ICallBack;
import com.hzdesk.media.koreabatchmatvector.inter.IModelCalBack;
import com.hzdesk.media.koreabatchmatvector.inter.NetworkRequestListener;
import com.hzdesk.media.koreabatchmatvector.utils.HttpUtil;
import com.hzdesk.media.koreabatchmatvector.utils.MacUtil;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/6/27.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class MyRequestModel {
    private static volatile MyRequestModel instance = null;
    private String sessionId="sessionId";
    private String sourceType="sourceType";
    public static final MediaType JSON= MediaType.parse("application/json; charset=utf-8");
    public OkHttpClient client = HttpUtil.getClient();
    private MyRequestModel() {}
    public static MyRequestModel getInstance(){
        if(instance == null){
            synchronized (MyRequestModel.class){
                if(instance == null){
                    instance = new MyRequestModel();
                }
            }
        }
        return instance;
    }
    /**
     * 异步get请求
     * 使用ICallBack接口传递返回数据
     * @param context
     * @param url
     * @return
     * @throws Exception
     */
    public void getSynchronized(final Context context, final String url, final IModelCalBack requestListener) {
        Request request = new Request.Builder()
                .url(url)
                .addHeader(sessionId, MacUtil.getMacAddress(context).toString().trim())
                /*.addHeader(sessionId, " ac:35:ee:22:88:12")*/
                .addHeader(sourceType,"1")
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                requestListener.fail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    requestListener.success(response);
                }
            }
        });

    }


    /**
     * post方式提交Map
     * 使用ICallBack接口传递返回数据
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public void postMap(final Context context, final String url, Map<String, String> map, final IModelCalBack requestListener) {
        FormBody.Builder builder = new FormBody.Builder();
        if (map != null) {
            //增强for循环遍历
            for (Map.Entry<String, String> entry : map.entrySet()) {
                builder.add(entry.getKey(), entry.getValue());
            }
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder().post(formBody).url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                requestListener.fail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    requestListener.success(response);
                }
            }
        });
    }
}
